package com.aspire.moss.portal.desktop;

import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.aspire.moss.portal.R;

/**
 * 工具类
 * @author Administrator
 *
 */
public class Utils {

	private static final String TAG = "Utils";
	
	private static final String APPINFO_DATA = "APPINFO_DATA";

	
	/**
	 * 弹出是否删除的提示框
	 * @param mContext
	 * @param controller 
	 * @param appInfo
	 */
	public static void createDeleteDailog(final Context mContext , final ViewController controller , final AppInfo appInfo){
		new AlertDialog.Builder(mContext)
		.setNegativeButton(mContext.getString(R.string.no), new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		})
		.setPositiveButton(mContext.getString(R.string.yes), new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				controller.removeAppView(appInfo , View.VISIBLE);
			}
		})
		.setTitle(mContext.getString(R.string.dialog_tip))
		.setMessage(mContext.getString(R.string.del_app_msg))
		.create()
		.show();
	}
	
	/**
	 * 把view转成bitmap
	 * @param view
	 * @return
	 */
	public final static Bitmap getViewBitmap(View view) {
        view.clearFocus();
        view.setPressed(false);

        final boolean willNotCache = view.willNotCacheDrawing();
        view.setWillNotCacheDrawing(false);

        // Reset the drawing cache background color to fully transparent
        // for the duration of this operation
        final int color = view.getDrawingCacheBackgroundColor();
        view.setDrawingCacheBackgroundColor(0);

        if (color != 0) {
            view.destroyDrawingCache();
        }
        view.buildDrawingCache();
        final Bitmap cacheBitmap = view.getDrawingCache();
        if (cacheBitmap == null) {
            Log.d(TAG, "failed getViewBitmap(" + view + ")");
            return null;
        }

        final Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

        // Restore the view
        view.destroyDrawingCache();
        view.setWillNotCacheDrawing(willNotCache);
        view.setDrawingCacheBackgroundColor(color);

        return bitmap;
    }
	
	/**
	 * 放大图片
	 * @param bitmap
	 * @param zoomInt
	 * @return
	 */
	public static Bitmap scaleBitmap(Bitmap bitmap , float zoomInt){
		if(null == bitmap) return null;
		final Matrix matrix = new Matrix();
		matrix.preScale(zoomInt , zoomInt);
		final Bitmap newBitmap = Bitmap.createBitmap(bitmap , 0 , 0 , bitmap.getWidth() , bitmap.getHeight() , matrix , false);
		return newBitmap;
	}
	
	/**
	 * 取得屏幕信息
	 * @param mContext
	 * @return
	 */
	public static DisplayMetrics getDisplayMetrics(Context mContext){
		return mContext.getResources().getDisplayMetrics();
	}
	
	public static int getScreenWidth(Context mContext){
		return getDisplayMetrics(mContext).widthPixels;
	}
	
	public static int getScreenHeight(Context mContext){
		return getDisplayMetrics(mContext).heightPixels;
	}
	
	public static LayoutInflater getLayoutInflater(Context mContext){
		return (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public static boolean isToLeft(int point){
		return point > 0 ? true : false;
	}
	
	public static boolean isToRight(int point){
		return point < 0 ? true : false;
	}
	
	/**
	 * 保存数据
	 * @param mContext
	 * @param appInfo
	 */
	public static void saveOrUpdateAppInfo(Context mContext , AppInfo appInfo){
		final SharedPreferences preferences = mContext.getSharedPreferences(APPINFO_DATA , Context.MODE_PRIVATE);
		preferences.edit().putString(appInfo.screen + "_" 
				+ appInfo.cellX 
				+ "*" + appInfo.cellY , appInfo.appInfoToJson()).commit();
		
	}
	
	/**
	 * 加载数据
	 * @param mContext
	 * @return
	 */
	public static Map<String, AppInfo>  getAppInfo(Context mContext){
//		final SharedPreferences preferences = mContext.getSharedPreferences(APPINFO_DATA , Context.MODE_PRIVATE);
//		final Map<String, AppInfo> map = new HashMap<String, AppInfo>();
//		final Map<String, String>  srcMap = (Map<String, String>) preferences.getAll();
//		Set<String> setKey = srcMap.keySet();
//		String appInfoJson = null;
//		AppInfo appInfo = null;
//		for(String key : setKey){
//			appInfoJson = srcMap.get(key);
//			appInfo = AppInfo.jsonToAppInfo(appInfoJson);
//			
//		}
		return null;
	}
	
}
